1) Git introduction

Published

Thu, 14 of March, 2024

Modified

Thu, 14 of March, 2024

Caution

Web page construction in progress…

Frist: some useful terminal commands

  • [shift+Q] –> to exit from git dialogue
  • highlight + [cmd + D] –> to seleect all subsequent instances
  • highlight + [alt + drag] –> to edit at begin of each lines
  • highlight + [cmd + shift + L] –> to edit at the end of each lines
  • highlight + [ctr + shift + W] –> wrapping text with start and end tag

Git installation

Git architecture

  • origin = stands for the remote repository. When we use git push -u origin local_branch, it tells the system that we want to push our local branch to the remote repository. Usually there is one default remote repository and origin represents this default repository.
    • If you don’t like “origin”, you can rename it
#  rename origin it by using 
git remote rename origin new_name
  • branch branch is a like a fork in the history of a repository. One branch represents an independent line of development, like a fork teeth.
#  to check which branches I have  
git branch -a
    # * master
    #   page_col
    #   remotes/origin/master
  • master master is a branch, the default branch, the main branch, and it’s always there.

  • HEAD = the currently active branch (the checked out one). Each repository only has one current branch, hence one HEAD as well.

    • Detached HEAD happens when a checkout command is applied to a specific historical commit, tag or remote branch.
#  to check where the HEAD of a repository is pointing to
cat .git/HEAD
    # ref: refs/heads/master
  • index index is the proposed next commit, also called staging area.

Git commands

See Figure 1

  • git status = View the state of working directory and staging area
  • git add = Stage changes for next commit
  • git commit = Commit the staged snapshot to the local repository
  • git push = Upload local repository content to a remote repository
    • 4 TEAM: it makes your local changes publicly available in a remote repository.
  • git clone = Copies an entire remote repository down to your local machine, setting up a cloned version and checks out the default branch (generally master)
    • this action is done only once.
  • git fetch = Download content from remote repository, but doesn’t force the merge
    • 4 TEAM: if a developer has pushed changes to a remote branch, those changes will be pulled down to your repository whenever fetch is performed.
      • Note: fetch won’t automatically merge any changes, only update references!
  • git merge = Join two branches together
  • git pull = Combo of git fetch and git merge
Figure 1: Git Workflow

Source: cloudstudio.com.au

Branches

Create & checkout a branch

“checkout” means to change the branch you are currently working on (or switch to)

# 1/2 create b
git branch page_col #create a new branch named "page_col"

# 2/2 then switch to b
git checkout page_col

# or 1+2/2 CREATE + SWITCH BRANCHES
git checkout -b page_col  

Now I see
Git chekout branch

Switch to other branch

You can also use git switch other_branch which is more specific

git switch page_col
cat .git/HEAD # (confirms me I moved)

Rename a (local) branch

It’s the -m parameter !

  • you cannot rename a remte branch –> you delete it and re-upload it
# In currently checkedout 
git branch -m better_name

# in different branch (non HEAD)
git switch master
git branch test_branch # fake one 
git branch -a  # it's there
git branch -m test_branch test_branch2
git branch -a  # yep!

Push upstream a local branch

  1. Create local branch
  2. Switch to local branch
  3. git push –-set-upstream command (the 1st time you push)
  4. Thereafter git push (all subsequent git push commands automatically move local branch changes up to the remote branch.)
# 1 Create local branch 
# 2 switch to local branch 
git switch page_col
# 3.a git push –set-upstream command (1st time)
git push  --set-upstream origin  page_col
  # check 
  git branch -a # YAY!
# 3.b git push origin (nest times )
git push origin

Rename a (remote) branch

You need to 1. Publish an existing local branch on remote git push -u origin local_branch 2. So you delete old one and push up a new one from local repository

Merge a git branch into master

  1. List All Git Branches
  2. Switch to Master
  3. Merge Branch into Master
Tip

Since merging is a type of commit, it also requires a commit message. There are two ways to specify the commit message:

# List
git branch
# Switch
git checkout master
# The merge creates a merge commit, bringing together multiple lines of development while preserving the history of the source branch.
git merge -m "Your merge commit message" branch_name

See differences b/w commits

# diff between 2 latest commits (1 on branch) only in files I care 
git diff dc87ae c86edffc16 "*.qmd"

#(To view diff between next commit (HEAD) and parent commit (SHA 682bc))
git diff dc87ae..  
git diff dc87ae^..HEAD  '***.qmd' #ORQ
git diff dc87ae..HEAD   #OR

# b/w old commit on branch and HEAD 
git diff 693e61^..HEAD

See differences b/w branches

git diff master..page_col '***.qmd'Q
## Difference b/w git fetch & git pull
## Difference b/w git merge & git rebase https://blog.git-init.com/differences-between-git-merge-and-rebase-and-why-you-should-care/

Collaboration

  1. I create a test GH account lula-test (associated to l__a__a@icloud.com)

  2. I clone in /Users/testuser/GH_test/nerd_help this repository “nerd-help” I own as Lulliter and (from there (I indicated that lula-test is a collaborator)

# positioned in parent folder /Users/testuser/GH_test/
git clone https://github.com/Lulliter/nerd_help.git
cd nerd_help

Rules

  • THE MASTER BRANCH SHOULD ALWAYS BE DEPLOYABLE
    • you create new branches for new features and merge them into Master when they’re completed.

    • It’s also important when collaborating that your team picks features that don’t have overlapping code.

  1. Here I create a new branch colors_page and I go there
#  I create a new branch to add a color.qmd page and switch there 
git checkout -b colors_page 

# verify this with the command:
git branch
  • “checkout” is used to switch between branches. Adding the “-b” and a name at the end creates a new branch and then moves into that new branch for us.

In the branch, the collabrorator

  • makes some changes …
  • add + commit + push

3.b Wait

Even if I am in the collaborator GH, lookg like the remote is considered the owner GH

git remote -v         
# origin  https://github.com/Lulliter/nerd_help.git (fetch)
# origin  https://github.com/Lulliter/nerd_help.git (push)

so I must change te origin in the collaborator GH

git remote set-url origin  https://github.com/lula-test/nerd_help.git

Reference